package com.codedef.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codedef.entity.Seat;
import com.codedef.entity.SeatVo;
import com.codedef.fservice.InitBloomService;
import com.codedef.mapper.SeatMapper;
import com.codedef.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLocalCachedMap;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SeatService {

    @Autowired
    SeatMapper seatMapper;

    @Autowired
    ClassroomService classroomService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    BloomCacheManager bloomCacheManager;

    @Autowired
    InitBloomService initBloomService;

    /**
     * 通过教室的ID，获取某一个教室内所有的座位
     * (该方法未来通过Redis缓存起来)
     */
    public List<Seat> getSeatListByClassroomId(Integer classroomId) {
        return seatMapper.selectList(new QueryWrapper<Seat>().eq("classroom_id", classroomId));
    }

    /**
     * 获得当前有预约记录的座位Id的列表
     */
    public List<Integer> getNowReservationSeatIdListByClassroomId(Integer classroomId) {
        return seatMapper.selectNowReservationSeatIdListByClassroomId(classroomId);
    }

    /**
     * 查询一个座位所属的教室id (将来使用Redis缓存起来)
     */
    public String getClassroomAbbreviationBySeatId(Integer seatId) {
        return seatMapper.selectClassroomAbbreviationBySeatId(seatId);
    }


    /**
     * 通过教室的ID，获取某一个教室内所有的座位
     * 包含当前预约状态
     */
    public List<Seat> getSeatListWithReservationByClassroomId(Integer classroomId) {
        Date start = new Date();
        // 查出该教室所有的座位
        List<Seat> seatList = this.getSeatListByClassroomId(classroomId);

        System.out.println("查询完数据库 " + (new Date().getTime() - start.getTime()));

        // 通过id查出该教室的Abbr
        String abbreviation = classroomService.getAbbreviationById(classroomId);
        // 查出该教室的座位状态 (从Redis)
//        RLocalCachedMap<Integer, Integer> cachedMap = redissonClient.getLocalCachedMap(abbreviation, LocalCachedMapOptions.defaults());
//        RMap<Integer, Integer> cachedMap = redissonClient.getMap(abbreviation);

        System.out.println("拿到RLocalCachedMap " + (new Date().getTime() - start.getTime()));

//        for (int i = 0; i < seatList.size(); i++) {
//            if (cachedMap.get(seatList.get(i).getId()) == 1)
//                seatList.get(i).setState(10);
//        }

//        for (int i = 0; i < seatList.size(); i++) {
//            log.debug( String.valueOf(seatList.get(i).getId()) );
//        }

        RMap<Integer, Integer> cachedMap = bloomCacheManager.getCachedMap(abbreviation);
        if (cachedMap.size() == 0 && seatList.size() != 0) {
            // Feign 调用InitBloom
            System.out.println(initBloomService.initSeatListBloomCache());
        }

        Map<Integer, Integer> readAllMap = cachedMap.readAllMap();

        seatList.forEach(seat -> {
            if (readAllMap.get(seat.getId()) == 10) {
                seat.setState(10);
                log.info("设置");
            }

        });


        Date end = new Date();

        System.out.println(DateUtil.getDateStr(start) + "  " + DateUtil.getDateStr(end));
        System.out.println(  (end.getTime() - start.getTime())  );
        return seatList;
    }


    /**
     * 获取一个时间段内, 所有空闲的座位列表
     * @param timeA 起始时间
     * @param timeB 结束时间
     * @return 该时间段空闲的作为列表
     */
    public List<SeatVo> getFreeSeatListByTimePeriod(Date timeA, Date timeB) {
        return seatMapper.selectFreeByTimePeriod(timeA, timeB);
    }

    /**
     * 通过Id查询一个座位
     * @param id 主键
     * @return Seat实体类
     */
    public Seat getSeatById(Integer id) {
        return seatMapper.selectOne(new QueryWrapper<Seat>().eq("id", id));
    }
}
